package gui;

import java.awt.Component;
import java.util.GregorianCalendar;

/**
 *
 * @author Pavel Lieberzeit
 */
public class GUIConstants {
    final public static java.awt.Color DISABLED_TEXT_COLOR = new java.awt.Color(153,153,153);
    final public static java.awt.Color BACKGROUND_ERROR_COLOR = new java.awt.Color(255,153,153);
    final public static java.awt.Color BACKGROUND_OKAY_COLOR = new java.awt.Color(255,255,255);
    
    
    public static void ukazChybu(Component component) {
        component.setBackground(GUIConstants.BACKGROUND_ERROR_COLOR);
    }
    
    public static void odznacChybu(Component component) {
        component.setBackground(GUIConstants.BACKGROUND_OKAY_COLOR);
    }
    
    /**
     * Overi casStraven, jestli to je kladne/nulove cele cislo
     * @return true pokud je chyba
     */
    public static boolean checkCasStraven(String cas) {
        if(cas.isEmpty())
            return true;
            
        for(int i = 0; i < cas.length(); i++) {
            if(!Character.isDigit(cas.charAt(i))) {
                    return true;
            }
        }
        return false;
    }
    
    /**
     * Overi deadline, zjisti, jestli je ve tvaru DD.MM.YYYY
     * Nekontroluje, jestli to nahodou neni uz prosly datum
     * @return true pokud je chyba
     */
    public static boolean checkDeadline(String datum) {
        
        if(datum.isEmpty())
            return false;
        
        int tecky[] = {-1,-1};
        int j = 0;
        for(int i = 0; i < datum.length(); i++) {
            if(!Character.isDigit(datum.charAt(i))) {
                if(datum.charAt(i) != '.')
                    return true;
                
                if(j > 1)
                    return true;
                
                tecky[j++]=i;               
            }
        }
        
        if(j != 2)
            return true;
        
        int mesic = Integer.parseInt(datum.substring(tecky[0]+1, tecky[1]));
        
        if(mesic < 1 || mesic > 12)
            return true;
        
        int rok = Integer.parseInt(datum.substring(tecky[1]+1, datum.length()));
        
        int den = Integer.parseInt(datum.substring(0, tecky[0]));
        
        if(den < 1)
            return true;
        
        switch(mesic) {
            case 1: if(den > 31)
                        return true;
                    break;
            case 2: if(rok % 4 == 0) {
                        if(rok % 100 == 0) {
                            if(rok % 400 == 0) {
                                if(den > 29)
                                    return true;
                            } else {
                                if(den > 28)
                                    return true;
                            }
                        } else {
                            if(den > 29)
                                return true;
                        }
                    } else
                        if(den > 28)
                            return true;
                    break;
                    //ugh, to by bylo
            case 3: if(den > 31)
                        return true;
                    break;
            case 4: if(den > 30)
                        return true;
                    break;
            case 5: if(den > 31)
                        return true;
                    break;
            case 6: if(den > 30)
                        return true;
                    break;    
            case 7: if(den > 31)
                        return true;
                    break;
            case 8: if(den > 31)
                        return true;
                    break;
            case 9: if(den > 30)
                        return true;
                    break; 
            case 10: if(den > 31)
                        return true;
                    break;
            case 11: if(den > 30)
                        return true;
                    break; 
            case 12: if(den > 31)
                        return true;
                    break;
            default: break; 
        }
        
        return false;
    }
    
    public static GregorianCalendar preprocDeadline(String datum) {
        
        GregorianCalendar dead = new GregorianCalendar();
        String den;
        String mesic;
        String rok;

        
        if(datum.isEmpty())
            return null;
        
        if(datum.charAt(1) == '.')
            den = datum.substring(0, 1);
        else
            den = datum.substring(0, 2); 
        
        if(datum.charAt(den.length() + 2) == '.') //pozice, kde je budto konec mesice nebo . za mesicem
            mesic = datum.substring(1+den.length(), 2+den.length());
        else
            mesic = datum.substring(1+den.length(), 3+den.length());
        
        rok = datum.substring(datum.length()-4, datum.length()); 

        dead.set(Integer.parseInt(rok), Integer.parseInt(mesic)-1, Integer.parseInt(den));
        
        return dead;
    }
    
}
